package com.sitanyun.merchant.guide.model.impl;

import com.google.gson.Gson;
import com.sitanyun.merchant.guide.bean.LoginBean;
import com.sitanyun.merchant.guide.bean.SmsPhoneBean;
import com.sitanyun.merchant.guide.callback.StringCallbacks;
import com.sitanyun.merchant.guide.model.inter.ILoginAModel;
import com.sitanyun.merchant.guide.presenter.callback.CallBack;
import com.sitanyun.merchant.guide.url.Urls;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;

/**
 * 网络请求M层
 * 验证码登录
 */
public class LoginAModelImpl implements ILoginAModel {
    @Override
    public <T> void setLoginphone(String phone, CallBack callBack) {
        OkHttpUtils.get().url(Urls.phoneverification+phone)

                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                SmsPhoneBean smsPhoneBean  = new Gson().fromJson(response, SmsPhoneBean.class);
             callBack.onSuccess(smsPhoneBean);
            }
        });
    }

    /**
     *    帐号密码登录
     * @param phone
     * @param password
     * @param callBack
     * @param <T>
     */
    @Override
    public <T> void setLoginpass(String phone, String password, CallBack callBack) {
        String urls=Urls.Login+"?username="+phone+"&password="+password+"&grant_type=password"+"&scope=server&s_client_id=MERCHANT"+"&privilege=GUIDE";
        OkHttpUtils.post().url(urls)
                .addHeader("Authorization","Basic dGVzdDp0ZXN0")
                .build().execute(new StringCallbacks() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                LoginBean loginBean  = new Gson().fromJson(response, LoginBean.class);
                callBack.onSuccess(loginBean);
            }
        });
    }
}
